graph set window fontface "Times New Roman"

* Figure 2 
use "data\out\pre_patterns_sample.dta", clear 

gcollapse (mean) lnforbruk forbruk lnforbruk_jan_march forbruk_jan_march lnforbruk_april_nov forbruk_april_nov (semean) se_lnforbruk = lnforbruk se_forbruk = forbruk se_lnforbruk_jan_march = lnforbruk_jan_march se_forbruk_jan_march = forbruk_jan_march se_lnforbruk_april_nov = lnforbruk_april_nov se_forbruk_april_nov = forbruk_april_nov, by(elcar hour) 
foreach x in lnforbruk forbruk lnforbruk_jan_march forbruk_jan_march lnforbruk_april_nov forbruk_april_nov {
	gen `x'_lo = `x'-1.96*se_`x'
	gen `x'_hi = `x'+1.96*se_`x'
}
egen yhi = max(forbruk_jan_march_hi) 
g y0 = 0 

sort elcar hour 

*** levels   
two (line forbruk_jan_march hour, c(L) lc(black) ) ///
  (rcap forbruk_jan_march_lo forbruk_jan_march_hi hour,  lc(black) lw(vthin)) ///
  ///(rarea yhi y0 hour if inrange(hour,16,22), col(gs11%20)) /// 
  , xline(16, lp(dash) lc(black)) xline(22, lp(dash) lc(black)) xlab(0(2)23, glwidth(vvthin)) ylab(0(1)4, glwidth(vvthin)) xtitle("") ///xline(16) xline(21) 
  legend(off) text(4.1 5.2 "With electric vehicle") text(2.9 5 "No electric vehicle") /// 
  title("January – March, 2019") /// 
  name(fig2_left_level, replace)

two (line forbruk_april_nov hour, c(L) lc(black) ) ///
  (rcap forbruk_april_nov_lo forbruk_april_nov_hi hour,  lc(black) lw(vthin)) ///
 /// (rarea yhi y0 hour if inrange(hour,16,22), col(gs11%20)) /// 
  , xline(16, lp(dash) lc(black)) xline(22, lp(dash) lc(black))  xlab(0(2)23, glwidth(vvthin)) ylab(0(1)4, glwidth(vvthin)) xtitle("") ///xline(16) xline(21) 
  legend(off) text(2.6 5.2 "With electric vehicle") text(1.6 5 "No electric vehicle") /// 
  title("April – November, 2019") /// 
  name(fig2_right_level, replace)

gr combine fig2_left_level fig2_right_level, iscale(*1.3) ycommon l1title("kWh", orient(horizontal) placement(12)) b1title(time of day) name(fig2_pre_patterns, replace)
graph export "output\fig2_pre_patterns.pdf", as(pdf) name("fig2_pre_patterns") replace


* Figure 3
use "data\out\estsample.dta", clear

g eventtime = 100 if inrange(treatday,1,7)
foreach d in 21893 21902 21937 21944 21958 21971 21979 {
  replace eventtime = 100 -1 if dato == `d' - 1 
  replace eventtime = 100 -2 if dato == `d' - 2 
  replace eventtime = 100 -3 if dato == `d' - 3 
  replace eventtime = 100 + 1 if dato == `d' + 1 
  replace eventtime = 100 + 2 if dato == `d' + 2 
}

keep if inrange(eventtime,97,102)
g forbruk = exp(lnforbruk)
gcollapse (mean) lnforbruk forbruk (semean) se_lnforbruk = lnforbruk se_forbruk = forbruk, by(eventtime Treat elcar hour) 
foreach x in lnforbruk forbruk {
	gen `x'_lo = `x'-1.96*se_`x'
	gen `x'_hi = `x'+1.96*se_`x'
}
egen yhi = max(forbruk_hi) 
g y0 = 0 

sort Treat elcar hour 

two (line forbruk hour if Treat == 1, c(L) lc(black) ) ///
  (line forbruk hour if Treat == 0, c(L) lc(gs9) lp(-) ) ///
  (rcap forbruk_lo forbruk_hi hour if Treat == 1,  lc(black) lw(vthin)) ///
  (rcap forbruk_lo forbruk_hi hour if Treat == 0,  lc(gs9) lw(vthin)) ///
 /// (rarea yhi y0 hour if inrange(hour,16,22), col(gs11%20)) /// 
  if eventtime == 100 /// 
  , xline(16, lp(dash) lc(black)) xline(22, lp(dash) lc(black))  xlab(0(2)23, glwidth(vvthin)) ylab(0(1)5, glwidth(vvthin)) ytitle("") xtitle("") ///
  legend(order(1 "Treatment" 2 "Control") pos(7) ring(0)) text(4.6 6 "With electric vehicle") text(2.95 5.8 "No electric vehicle") /// 
  title(CPP day) /// 
  name(fig2_extra_mean_T_C_elcar_others, replace)
*graph export "H:\Dokumenter\strøm\JAERE revision\fig2_extra_mean_T_C_elcar_others.pdf", as(pdf) name("fig2_extra_mean_T_C_elcar_others") replace

two (line forbruk hour if Treat == 1, c(L) lc(black) ) ///
  (line forbruk hour if Treat == 0, c(L) lc(gs9) lp(-) ) ///
  (rcap forbruk_lo forbruk_hi hour if Treat == 1,  lc(black) lw(vthin)) ///
  (rcap forbruk_lo forbruk_hi hour if Treat == 0,  lc(gs9) lw(vthin)) ///
  ///(rarea yhi y0 hour if inrange(hour,16,22), col(gs11%20)) /// 
  if eventtime == 101 /// 
  , xline(16, lp(dash) lc(black)) xline(22, lp(dash) lc(black))  xlab(0(2)23, glwidth(vvthin)) ylab(0(1)5, glwidth(vvthin)) xtitle("") ///
  legend(off) text(4.6 6 "With electric vehicle") text(2.95 5.8 "No electric vehicle") /// 
  ///text(4.9 18.5 "Control") text(3.05 18.5 "Control") text(3.5 18.5 "Treatment") text(2.3 18.5 "Treatment") ///
  title(Day after CCP day) /// 
  name(fig2_extra_mean_T_C_elcar_oth_p1, replace)
*graph export "H:\Dokumenter\strøm\JAERE revision\fig2_extra_mean_T_C_elcar_oth_p1.pdf", as(pdf) name("fig2_extra_mean_T_C_elcar_oth_p1") replace

gr combine fig2_extra_mean_T_C_elcar_others fig2_extra_mean_T_C_elcar_oth_p1, iscale(*1.3) b1title(time of day) l1title("kWh", orient(horizontal) placement(12)) name(fig3_event_post1mean, replace)
graph export "output\fig3_event_post1mean.pdf", as(pdf) name("fig3_event_post1mean") replace
